package com.scs.controller;

import com.alibaba.fastjson.JSONObject;
import com.scs.pojo.StudentLeave;
import com.scs.pojo.classInfo;
import com.scs.pojo.student;
import com.scs.service.ClassInfoService;
import com.scs.service.StudentLeaveService;
import com.scs.service.studentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("StudentLeave")
public class StudentLeaveController {
    @Autowired
    private StudentLeaveService studentLeaveService;
    @Autowired
    private studentService studentService;
    @Autowired
    private ClassInfoService classInfoService;
    /**
     * 学生请假接口
     * @param request
     * @return
     */
    @RequestMapping(value = "/insertInfo", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String insertInfo(HttpServletRequest request){
        //获取请假人
        String userId = (String) request.getSession().getAttribute("userInformation");
        //请假开始时间
        String startTime = request.getParameter("startTime");
        //请假结束时间
        String endTime = request.getParameter("endTime");
        //请假时有
        String leaveReason = request.getParameter("leaveReason");
        if (leaveReason == null || leaveReason.equals("")){
            leaveReason = "（无）";
        }
        JSONObject data = new JSONObject();
        if(userId==null||userId.equals("")){
            data.put("msg","学生信息获取失败");
            data.put("data","");
            data.put("success",0);
            data.put("code",110);
            return data.toJSONString();
        }
        if(startTime==null||endTime==null||leaveReason==null||startTime.equals("")||endTime.equals("")||leaveReason.equals("")){
            data.put("msg","请假信息填写不完整");
            data.put("data","");
            data.put("success",0);
            data.put("code",110) ;
            return data.toJSONString();
        }
        //获取这个学生的辅导员
        List<student> student = studentService.getStudentById(userId);
        String realName = student.get(0).getRealName();
        String classes = student.get(0).getClasses();
        String major = student.get(0).getMajor();
        classInfo classInfo = classInfoService.getInfo(classes, major);
        String counselorId = classInfo.getCounselorId();
        StudentLeave studentLeave = new StudentLeave(null, userId,realName,classes, startTime, endTime, leaveReason, null, 1,counselorId);
        int insertNumber = studentLeaveService.insertInfo(studentLeave);
        if (insertNumber==0){
            data.put("msg","提交请假申请失败");
            data.put("data","");
            data.put("success",0);
            data.put("code",110);
        }
        else{
            data.put("msg","提交请假申请成功");
            data.put("data","");
            data.put("success",1);
            data.put("code",200);
        }
        return data.toJSONString();
    }

    /**
     * 用于辅导员获取学生请假申请
     * @param request
     * @return
     */
    @RequestMapping(value = "/getLeaveInfo", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getLeaveInfo(HttpServletRequest request){
        JSONObject data = new JSONObject();
        //获得当前辅导员
        String userId= (String) request.getSession().getAttribute("userInformation");
        if(userId==null||userId.equals("")){
            data.put("msg","获取辅导员失败");
            data.put("data","");
            data.put("count",0);
            data.put("code",110);
            return data.toJSONString();
        }
        //获得该辅导员需要审批的假条
        List<StudentLeave> leaveOfStu = studentLeaveService.getLeaveOfStu(1, userId);
        if(leaveOfStu.size()==0){
            data.put("msg","您还没有假条处理");
            data.put("data","");
            data.put("count",0);
            data.put("code",110);
        }
        else{
            data.put("msg","学生假条信息");
            data.put("data",leaveOfStu);
        }
        data.put("count",leaveOfStu.size());
        data.put("code",0);
        return data.toJSONString();
    }

    /**
     * 老师批假
     * @param request
     * @return
     */
    @RequestMapping(value = "/updateInfo", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String updateInfo(HttpServletRequest request){
        JSONObject data = new JSONObject();
        Integer status = Integer.parseInt(request.getParameter("status")) ;
        int leaveId = Integer.parseInt(request.getParameter("leaveId"));
        String rejectInfo = request.getParameter("rejectInfo");
        if(status.equals("")||status==null){
            data.put("msg","获取status为空");
            data.put("data","");
            data.put("success",0);
            data.put("code",110);
            return data.toJSONString();
        }
        int updateCount = studentLeaveService.updateStatus(rejectInfo, status, leaveId);
        if(updateCount!=0){
            data.put("msg","审批成功");
            data.put("data","");
            data.put("success",1);
            data.put("code",200);
            return data.toJSONString();
        }
        data.put("msg","审批失败");
        data.put("data","");
        data.put("success",0);
        data.put("code",110);
        return data.toJSONString();
    }


    @RequestMapping(value = "/getMyLeaveInfo", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getMyLeaveInfo(HttpServletRequest request){
        JSONObject data = new JSONObject();
        String userId = (String) request.getSession().getAttribute("userInformation");
        if(userId==null||userId.equals("")){
            data.put("msg","用户信息失效，请重新登录");
            data.put("data","");
            data.put("count",0);
            data.put("code",110);
            return data.toJSONString();
        }
        List<StudentLeave> myLeaveInfo = studentLeaveService.getMyLeaveInfo(userId);
        if(myLeaveInfo.size()==0){
            data.put("msg","暂无请假信息");
            data.put("data","");
            data.put("count",0);
            data.put("code",0);
            return data.toJSONString();
        }
        data.put("msg","获取成功");
        data.put("data",myLeaveInfo);
        data.put("count",myLeaveInfo.size());
        data.put("code",0);
        return data.toJSONString();
    }

    @RequestMapping(value = "/getMyLeaveRecordOfStu", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getMyLeaveRecordOfStu(HttpServletRequest request){
        JSONObject data = new JSONObject();
        String userId = (String) request.getSession().getAttribute("userInformation");
        if(userId==null||userId.equals("")){
            data.put("msg","用户信息失效，请重新登录");
            data.put("data","");
            data.put("count",0);
            data.put("code",110);
            return data.toJSONString();
        }
        List<StudentLeave> myLeaveInfo = studentLeaveService.getMyLeaveRecordOfStu(userId);
        if(myLeaveInfo.size()==0){
            data.put("msg","暂无请假信息");
            data.put("data","");
            data.put("count",0);
            data.put("code",0);
            return data.toJSONString();
        }
        data.put("msg","获取成功");
        data.put("data",myLeaveInfo);
        data.put("count",myLeaveInfo.size());
        data.put("code",0);
        return data.toJSONString();
    }
}
